#!/bin/sh -e

patch -p1 < fix-stdio-usage.patch
patch -p1 < disable-symver.patch
patch -p1 < bashism.patch

# LVM2 calls 'fmt -1' throughout the build process.
# Busybox doesn't provide this command, however
# all it does is split words one per line and
# 'tr' can be used in its place.
sed -i 's/fmt -1/tr " " "\\n"/' \
    make.tmpl.in libdm/make.tmpl.in

# The build fails when using busybox's 'mkdir'(?),
# swap to using 'install -d' instead which works.
export MKDIR_P="install -d"

# Bashism banished.
export CONFIG_SHELL=/bin/sh

pkg-config --exists libudev || udev=disable

./configure \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --disable-selinux \
    --enable-pkgconfig \
    --enable-fsadm \
    --enable-dmeventd \
    --enable-cmdlib \
    --disable-symvers \
    --with-default-dm-run-dir=/run \
    --with-default-locking-dir=/run/lock/lvm \
    --with-default-pid-dir=/run \
    --with-default-run-dir=/run/lvm \
    --"${udev:=enable}"-udev_sync \
    --"$udev"-udev_rules

make DESTDIR="$1" install

# Make all libraries and binaries writable.
chmod u+w "$1/usr/lib/"* \
          "$1/usr/include/"* \
          "$1/usr/bin/"*
